package com.yht.redbook.user.relation.biz.rpc;

import cn.hutool.core.collection.CollUtil;
import com.yht.framework.common.response.Response;
import com.yht.redbook.user.api.UserFeignApi;
import com.yht.redbook.user.dto.req.FindUserByIdReqDTO;
import com.yht.redbook.user.dto.req.FindUsersByIdReqDTO;
import com.yht.redbook.user.dto.resp.FindUserByIdRspDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component
public class UserRpcService {

    @Resource
    private UserFeignApi userFeignApi;

    public FindUserByIdRspDTO findUserById(Long followUserId) {
        FindUserByIdReqDTO findUserByIdReqDTO = new FindUserByIdReqDTO();
        findUserByIdReqDTO.setId(followUserId);

        Response<FindUserByIdRspDTO> response = userFeignApi.findUserById(findUserByIdReqDTO);

        if (!response.isSuccess() || Objects.isNull(response.getData())) {
            return null;
        }
        return response.getData();
    }

    public List<FindUserByIdRspDTO> findUsersById(List<Long> ids) {
        FindUsersByIdReqDTO findUsersByIdReqDTO = FindUsersByIdReqDTO.builder()
                .ids(ids)
                .build();

        Response<List<FindUserByIdRspDTO>> usersList = userFeignApi.findUsersById(findUsersByIdReqDTO);

        if (!usersList.isSuccess() || Objects.isNull(usersList.getData()) || CollUtil.isEmpty(usersList.getData())) {
            return null;
        }
        return usersList.getData();
    }
}
